package demo.jms.listener;


import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.jms.listener.SessionAwareMessageListener;

/**
 * spring提供的消息监听器
 * 
 * @author JSC
 *
 */
public class ConsumerSessionAwareMessageListener implements SessionAwareMessageListener {
	/** 接受到消息后发送回去的目的地 */
	@Resource(name = "replyQueueDestination")
	private Destination destination;

	/**
	 * 与MessageListener的不同是有一个Session参数，可以用来发送消息
	 */
	@Override
	public void onMessage(Message message, Session session) throws JMSException {
		TextMessage textMsg = (TextMessage) message;// 转换成文本消息
		try {
			System.out.println("[ConsumerSessionAwareMessageListener接收到一条消息] " + textMsg.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
		System.out.println("[ConsumerSessionAwareMessageListener回复] 原消息：" + textMsg.getText());
		/* jms发送消息的方式 */
		MessageProducer messageProducer = session.createProducer(destination);
		TextMessage textMessage = session
				.createTextMessage(textMsg.getText());
		messageProducer.send(destination, textMessage);
	}

}
